package com.clw.controller;


import com.clw.converter.DictionaryForm2DictionaryDTOConverter;
import com.clw.dataobject.DataDictionary;
import com.clw.dataobject.DataDictionaryDetail;
import com.clw.dto.DictionaryDTO;
import com.clw.enums.ResultEnum;
import com.clw.form.DataDictionaryForm;
import com.clw.service.DataDictionaryService;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MYQ on 2017/10/27.
 */
@Controller
@RequestMapping("/dictionary")
public class DataDictionaryController {

    @Autowired
    private DataDictionaryService service;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("data_dictionary/index");
    }

    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String code=request.getParameter("code");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        Map<String,Object> map=new HashMap<>();
        Page<DataDictionary> dataDictionaryPage=service.findAll(name,code,startTime,endTime,request1);
        map.put("rows",dataDictionaryPage.getContent());
        map.put("total",service.getSize(name,code,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) String id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            DataDictionary dataDictionary=service.findOne(Integer.valueOf(id));
            map.put("dataDictionary",dataDictionary);
        }
        return new ModelAndView("data_dictionary/add_view");
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(@Valid DataDictionaryForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        DictionaryDTO dictionaryDTO= DictionaryForm2DictionaryDTOConverter.convert(form);
        if (!StringUtils.isNotEmpty(form.getId())) {
            DictionaryDTO result = service.save(dictionaryDTO);
        }
        else {
            DictionaryDTO result=service.update(dictionaryDTO);
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = true) Integer id){
        DataDictionary dataDictionary=service.findOne(id);
        if (dataDictionary==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        service.del(dataDictionary);
        return ResultVOUtils.success("删除成功");
    }

    @ResponseBody
    @RequestMapping("/getDetail")
    public List<DataDictionaryDetail> getDetail(@RequestParam("id") Integer id, Map<String, Object> map){
        List<DataDictionaryDetail> details=service.findDetail(id);
        return details;
    }

    @ResponseBody
    @RequestMapping("/findType")
    public List<DataDictionaryDetail> findType(@RequestParam("code") String code){
        List<DataDictionaryDetail> details=service.getDetailByCode(code);
        return details;
    }
}
